<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\YihuService;
use Illuminate\Http\Request;

class YihuController extends BaseController
{
    //医护

    public function __construct(YihuService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption()
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //医生列表
    public function doctorList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['doc_name'] = $request->post('doc_name', '');
        $res = $this->db->doctorList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //医生详情
    public function doctorOne(Request $request)
    {
        $did = $request->post('did', 0);
        $res = $this->db->doctorOne($did);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

//    //医院模糊搜索
//    public function yiyuanByKey(Request $request)
//    {
//        $key = $request->post('key', '');
//        $res = $this->db->yiyuanByKey($key);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson(0, '', $res['data']);
//    }

    //预约时间选项
    public function yytimeList(Request $request)
    {
        $did = $request->post('doc_uid', 0);
        $res = $this->db->getYYtime($did);
        return $this->toJson(0, '', $res);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
//        $pas['type'] = $request->post('type', 0); //代办服务ID
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0); //价格元
        $pas['yytime'] = $request->post('yytime', ''); //预约时间
        $pas['yydizhi'] = $request->post('yydizhi', ''); //上门地址
        $pas['remark'] = $request->post('remark', '');
        $pas['yy_map'] = $request->post('yy_map', ''); //预约时经纬度
        $attrList = $request->post('attrList', ''); //属性JSON字符串
        $pas['price'] = $pas['price'] ? $pas['price'] : 0;
        if (mb_substr($pas['yytime'], -6) == '(已有预约)') {
            $pas['yytime'] = mb_substr($pas['yytime'], 0, 10);
        }
//        if (!$pas['type']) {
//            return $this->toJson(-2, '服务类型必须的');
//        }
        if (!$pas['yytime']) {
            return $this->toJson(-2, '预约时间必须的');
        }
        $res = $this->db->add($pas, $attrList);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //列表
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $res = $this->db->getList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 医生接单
     */
    public function rob(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->doctorRob($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 病情评估
     */
    public function see(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->doctorSee($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 上门服务
     */
    public function door(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->toDoor($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 取消
     */
    public function close(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $close_remark = $request->post('remark', '');
        $res = $this->db->toClose($id, $close_remark);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 服务确认
     */
    public function sure(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', 0);
        $res = $this->db->toSure($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 患者预约
     */
    public function patYuyue(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['type'] = $request->post('cate_id', 0);
        $pas['did'] = $request->post('doc_uid', 0);
        $pas['yytime'] = $request->post('yytime', '');
        $pas['yydizhi'] = $request->post('yydizhi', '');
        $pas['remark'] = $request->post('remark', '');
        $pas['yy_map'] = $request->post('yy_map', ''); //预约时经纬度
        if (!$pas['type']) {
            return $this->toJson(-2, '类型必须的');
        }
        if (!$pas['did']) {
            return $this->toJson(-2, '医生必须的');
        }
        if (!$pas['yytime']) {
            return $this->toJson(-2, '预约时间必须的');
        }
        if (!$pas['yydizhi']) {
            return $this->toJson(-2, '预约地址必须的');
        }
        $res = $this->db->patYuyue($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 小区医护价格
     * 通过类型获取
     */
    public function price(Request $request)
    {
        $cate_id = $request->post('cate_id', 0);
        if (!$cate_id) {
            return $this->toJson(-2, '类型必须的');
        }
        $res = $this->db->price($cate_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 医护详情
     */
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}